﻿/***********************************************************************
 * <copyright file="AccountPresenter.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   BangNC
 * Email:    BangNC@buca.vn
 * Website:
 * Create Date: Thursday, March 13, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.View.Dictionary;


namespace WindowsForms.AOnline.Presenter.Dictionary.Account
{
    public class AccountPresenter : Presenter<IAccountView>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AccountPresenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public AccountPresenter(IAccountView view)
            : base(view)
        {
        }

        /// <summary>
        /// Displays the specified account identifier.
        /// </summary>
        /// <param name="accountId">The account identifier.</param>
        public void Display(string accountId)
        {
            if (accountId == null) { View.AccountId = 0; return; }

            var account = Model.GetAccount(int.Parse(accountId));
            View.AccountId = account.AccountId;
            View.UnitId = account.UnitId;
            View.AccountCode = account.AccountCode;
            View.AccountName = account.AccountName;
            View.ParentId = account.ParentId;
            View.Grade = account.Grade;
            View.IsDetail = account.IsDetail;
            View.Description = account.Description;       
            View.IsCustomer = account.IsCustomer;
            View.IsVendor = account.IsVendor;
            View.IsTax = account.IsTax;
            View.IsInventoryItem = account.IsInventoryItem;
            View.IsFixedAsset = account.IsFixedAsset;
            View.IsActive = account.IsActive;
            View.IsSystem = account.IsSystem;
            View.BalanceSide = account.BalanceSide;
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns></returns>
        public string Save()
        {
            var account = new AccountModel
            {
                AccountId = View.AccountId,
                UnitId = View.UnitId,
                AccountCode = View.AccountCode,
                AccountName = View.AccountName,
                ParentId = View.ParentId,
                Grade = View.Grade,
                IsDetail = View.IsDetail,
                Description = View.Description,
                IsCustomer = View.IsCustomer,
                IsVendor = View.IsVendor,
                IsTax = View.IsTax,
                IsInventoryItem = View.IsInventoryItem,
                IsFixedAsset = View.IsFixedAsset,
                IsActive = View.IsActive,
                IsSystem = View.IsSystem,
                BalanceSide = View.BalanceSide, 
            };

            if (View.AccountId == 0)
                return Model.AddAccount(account);
            return Model.UpdateAccount(account);
        }

        /// <summary>
        /// Deletes the specified accont identifier.
        /// </summary>
        /// <param name="accountId">The accont identifier.</param>
        /// <returns></returns>
        public string Delete(int accountId)
        {
            return Model.DeleteAccount(accountId);
        }
    }
}
